Git

CLI
tool
versioning
Git is a version control system mostly used for code.

Configuration

Set username (for your commits)
git config --global user.name "myname"
Set email (for your commits)
git config --global user.email "my@email.com"
Enable CLI color codes
git config --global color.ui auto

New repositories

Download git repo from server
git clone url.com

Changes

Code change workflow

  1. Get changes from remote repository
    git fetch
  2. Check differences to branch you want merge into yours
    git diff feature1..dev
  3. Merge changes from remote branch into your branch
    git merge origin master
  4. Check what files are(n’t) staged for commit
    git status
  5. Stage all files that are tracked (not in .gitignore)
    git add .
    If necessary: Unstage all files (or a specific file) that are staged
     git reset [specific-file]
  6. Commit all staged files (add to versioning history)
    git commit -m "[ticket-id + what you changed]"
  7. Load commits into remote git repository
    git push

Managing changes

See changes due to last pull request
git log -p -2

Pull changes into dev/test/master branch

Pull changes into dev branch (and add your changes at the end)
git fetch
git checkout dev
git pull --rebase feature1branch 
Pull only specific commits into your branch
git cherry-pick [commit-SHA]

Undo and Rollback

Rollback the last commit
git reset head~1

To provide the files from the last commit: git reset --soft HEAD~1

Rollback to a specific commit
git reset [commit-SHA]
You can still see the changes from your HEAD in your files. To make the reset materialize, you need to discard these unstaged changes in git.

Since you are now several commits behind the remote, you have to force push the changes: git push -f origin [myBranchyBranch].

To create a new commit to undo earlier commits, use git revert.

Travel back to a specific commit
git checkout [commit-SHA]
Delete sensitive data from history:
docs.github.com

Branches

List all branches and show which one you are on
git branch -a
Create new branch and check it out
git checkout -b [branch-name]
Change to a specific branch
git checkout [specific-branch]
Delete a specific branch
git branch -d [specific-branch]
Rename the current branch
git branch -m myNewBranch

History

Show commit history with branch dependencies
git log --graph --oneline